package com.dtw.ditouwang.ui.movement;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.dtw.ditouwang.R;
import com.dtw.ditouwang.beans.ChallengeListBean;
import com.dtw.ditouwang.ui.BaseActivity;
import com.dtw.ditouwang.ui.OnItemClickListener;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class MovementChallengeActivity extends BaseActivity implements OnItemClickListener {
    public final static String KeyChallengeBean = "KeyChallengeBean";
    public static final String KeyTypeRoom="KeyTypeRoom";
    public static final String KeyTypeWalk="KeyTypeWalk";
    ChallengeAdapter challengeAdapter;
    List<ChallengeListBean.DataBean> challengeList = new ArrayList<>();
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_movemnt_challenge);
        enableBack();
        String roomtype = "室内";
        String walkType = "健走";
        if (getIntent().getIntExtra(KeyTypeRoom,0)!=0){
            roomtype="室外";
        }
        if(getIntent().getIntExtra(KeyTypeWalk,0)!=0){
            walkType = "跑步";
        }
        setTitle(roomtype + walkType);
        final RecyclerView recyclerView = findViewById(R.id.recyclerview);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        challengeAdapter = new ChallengeAdapter(challengeList);
        challengeAdapter.setOnItemClickListener(this);
        recyclerView.setAdapter(challengeAdapter);

        webApi.getChallengeList(getIntent().getIntExtra(KeyTypeWalk,0)+1).enqueue(new Callback<ChallengeListBean>() {
            @Override
            public void onResponse(Call<ChallengeListBean> call, Response<ChallengeListBean> response) {
                if(response.body().isResult()) {
                    challengeList.clear();
                    challengeList.addAll(response.body().getData());
                    challengeAdapter.notifyDataSetChanged();
                }
            }

            @Override
            public void onFailure(Call<ChallengeListBean> call, Throwable t) {

            }
        });
    }

    @Override
    public void onItemClick(int resID, int position, int state) {
        challengeList.get(position).setWalk(getIntent().getIntExtra(KeyTypeWalk,0)==0);
        challengeList.get(position).setChallenge(true);
        if(getIntent().getIntExtra(KeyTypeRoom,0)!=0){
            Intent intent = new Intent(this, MovementOutSideActivity.class);
            intent.putExtra(KeyChallengeBean,challengeList.get(position));
            startActivity(intent);
            finish();
        }else{
            Intent intent = new Intent(this, MovementInsideActivity.class);
            intent.putExtra(KeyChallengeBean,challengeList.get(position));
            startActivity(intent);
            finish();
        }
    }
}
